﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Quellenverwaltung.Data.Source
{
    public class Author
    {
        public Author()
        {
        }

        public Author(String names)
        {
            NameListAsString = names;
        }

        [XmlArray(ElementName = "NameList", Namespace = "http://schemas.microsoft.com/office/word/2004/10/bibliography")]
        public List<Person> NameList { get; set; }
        public bool ShouldSerializeNameList() { return this.NameList != null; }

        [XmlElement(ElementName = "Corporate", Namespace = "http://schemas.microsoft.com/office/word/2004/10/bibliography")]
        public String Corporate { get; set; }
        public bool ShouldSerializeCorporate() { return (this.Corporate != null && this.Corporate.Length > 0); } 

        [XmlIgnore]
        public String NameListAsString
        {
            get
            {
                if (NameList != null)
                {
                    String str = "";

                    for (int i = 0; i < NameList.Count; i++)
                    {
                        str += NameList[i].PersonAsString;
                        if (i < NameList.Count - 1)
                            str += "; ";
                    }

                    return str;
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (NameList == null)
                {
                    NameList = new List<Person>();
                }

                NameList = new List<Person>();
                if (!String.IsNullOrEmpty(value))
                {
                    String[] nameList = value.Split(';');
                    foreach (String name in nameList)
                    {
                        if (name.Count() > 3)
                            NameList.Add(new Person(name.Trim()));
                    }
                }
            }
        }
    }
}
